import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbactivityfollow } from '../../../atoms/db';

const logger = anylogger('pages/api/activity/follow.api');

export type IData = ITbactivityfollow;


export type Result = {
	count: number;
	data: IData[];
};

export type Query = {
	/**
	 * 用户id
	 */
	userid: string;
}

/**
 * 参与的活动-查询
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	try {
		logger.debug('msg query:', req.query);
		const { userid, ...rest } = req.query as Query;
		//获取数据
		const dtData = tbactivityfollow();
		dtData.where({
			userid
		});
		const d = await dtData.select('*')
			.orderBy('followtime', 'asc');
		//返回数据
		const data = d.map((it) => {
			return {
				...it
			} as IData;
		});
		res.status(200).json({ data, count: data.length });
	} catch (error) {
		logger.trace(error);
		logger.error(error);
		res.status(200).json({
			data: [],
			count: 0
		});
	}
});

export const config = {} as PageConfig;

export default handler;
